package br.com.unioeste.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import br.com.unioeste.bo.Curso;
import br.com.unioeste.bo.Disciplina;
import br.com.unioeste.bo.Semestre;
import br.com.unioeste.bo.Serie;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ListaDisciplinas.java
 *
 * Created on 07/08/2011, 19:52:14
 */


/**
 *
 * @author diogo
 */
public class ListaDisciplinas extends JPanel implements TreeSelectionListener {

	private Map<String, Semestre[]> horario;
	
    /** Creates new form ListaDisciplinas */
    public ListaDisciplinas() {
        initComponents();
    }
    
    public ListaDisciplinas(List<Curso> listaCursos){
    	
    	horario = new HashMap<String, Semestre[]>();
    	
    	DefaultMutableTreeNode top = new DefaultMutableTreeNode("CECE");
    	
    	for (Curso curso : listaCursos) {
    		DefaultMutableTreeNode cursoNode = new DefaultMutableTreeNode(curso.getNome());
			for(Serie serie : curso.getSeries()){
				DefaultMutableTreeNode serieNode = new DefaultMutableTreeNode(serie.getSerie());
				if(serie.getSemestre() != null)
					horario.put(curso.getNome()+serie.getSerie(), serie.getSemestre());
				
				for(Disciplina disciplina : serie.getDisciplinas()){
					DefaultMutableTreeNode disciplinaNode = new DefaultMutableTreeNode(disciplina.getNomeDisciplina());
					serieNode.add(disciplinaNode);
				}
				cursoNode.add(serieNode);
			}
	    	top.add(cursoNode);
		}
		
		jTree1 = new JTree(top);
		jTree1.addTreeSelectionListener(this);
		
		setLayout(new GridBagLayout());
		
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;

        jScrollPane1 = new JScrollPane(jTree1);

        this.add(jScrollPane1 , gridBagConstraints);
         	
    }

    
    private void initComponents() {

        jScrollPane1 = new JScrollPane();
        jTree1 = new JTree();

        GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;

        jScrollPane1 = new JScrollPane(jTree1);

        this.add(jScrollPane1 , gridBagConstraints);
        
    }
    
    private JScrollPane jScrollPane1;
    private JTree jTree1;
    
	@Override
	public void valueChanged(TreeSelectionEvent evt) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) 
					jTree1.getLastSelectedPathComponent();
		
		if(node.getLevel() == 2){
			String key = node.getParent().toString()+node.toString();
			Semestre[] semestres = horario.get(key);
			if(semestres != null){
    			this.removeAll();
    			GridBagConstraints gridBagConstraints = new GridBagConstraints();
				gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
		        gridBagConstraints.ipadx = 100;
		        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;

		        this.add(jScrollPane1 , gridBagConstraints);
		        
		        gridBagConstraints = new GridBagConstraints();
		        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		        gridBagConstraints.anchor = GridBagConstraints.CENTER;
		        
				this.add(new ListaHorarioSerie(semestres, key),gridBagConstraints);
    			this.getParent().validate();
    			
			}
			else {
				this.removeAll();
				GridBagConstraints gridBagConstraints = new GridBagConstraints();
				gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
		        gridBagConstraints.ipadx = 100;
		        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;

		        this.add(jScrollPane1 , gridBagConstraints);
		        this.getParent().validate();
			}
		}else{
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
	        gridBagConstraints.ipadx = 100;
	        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;

	        this.add(jScrollPane1 , gridBagConstraints);
	        this.getParent().validate();
		}

		
	}
}
